import shutil
import os

# 目标文件
jobtxt = r"E:\python\test\实例\text2.txt"
# 创建一个临时文件
tmp_file = r"E:\python\test\实例\text_opt.txt"


with open(jobtxt, 'r', encoding='utf-8') as src_job,\
    open(tmp_file, mode="w" , encoding='utf-8') as tmp_obj:
    for line in src_job:
        if "ssh" in line:
            new_line = line.replace("ssh", "shell")
            tmp_obj.write(new_line)
        else:
            tmp_obj.write(line)
    print("替换完成")

shutil.copy(tmp_file, jobtxt)
os.remove(tmp_file)

